package com.ruoyi.asset.service.impl;


import java.util.List;


import com.ruoyi.asset.domain.AssetData;
import com.ruoyi.asset.domain.AssetDepreciation;
import com.ruoyi.asset.mapper.AssetDataMapper;
import com.ruoyi.asset.mapper.AssetDepreciationMapper;
import com.ruoyi.asset.menu.ErrorCode;
import com.ruoyi.common.core.domain.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.asset.mapper.AssetInfoMapper;
import com.ruoyi.asset.domain.AssetInfo;
import com.ruoyi.asset.service.IAssetInfoService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 资产基本信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-03-12
 */
@Service
public class AssetInfoServiceImpl implements IAssetInfoService 
{
    @Autowired
    private AssetInfoMapper assetInfoMapper;
    @Autowired
    private AssetDataMapper assetDataMapper;
    @Autowired
    private AssetDepreciationMapper assetDepreciationMapper;
    /**
     * 查询资产基本信息
     * 
     * @param id 资产基本信息主键
     * @return 资产基本信息
     */
    @Override
    public AssetInfo selectAssetInfoById(Long id)
    {
        return assetInfoMapper.selectAssetInfoById(id);
    }

    /**
     * 查询资产基本信息列表
     * 
     * @param assetInfo 资产基本信息
     * @return 资产基本信息
     */
    @Override
    public List<AssetInfo> selectAssetInfoList(AssetInfo assetInfo)
    {
        return assetInfoMapper.selectAssetInfoList(assetInfo);
    }

    /**
     * 新增资产基本信息
     * 
     * @param assetInfo 资产基本信息
     * @return 结果
     */
    @Override
    public int insertAssetInfo(AssetInfo assetInfo)
    {
        return assetInfoMapper.insertAssetInfo(assetInfo);
    }

    /**
     * 修改资产基本信息
     * 
     * @param assetInfo 资产基本信息
     * @return 结果
     */
    @Override
    public int updateAssetInfo(AssetInfo assetInfo)
    {
        return assetInfoMapper.updateAssetInfo(assetInfo);
    }

    /**
     * 批量删除资产基本信息
     * 
     * @param ids 需要删除的资产基本信息主键
     * @return 结果
     */
    @Override
    public int deleteAssetInfoByIds(Long[] ids)
    {
        return assetInfoMapper.deleteAssetInfoByIds(ids);
    }

    /**
     * 删除资产基本信息信息
     * 
     * @param id 资产基本信息主键
     * @return 结果
     */
    @Override
    public int deleteAssetInfoById(Long id)
    {
        return assetInfoMapper.deleteAssetInfoById(id);
    }

    @Override
    @Transactional
    public R changeAssetInfo(AssetInfo assetInfo) {



       if (assetInfo != null && assetInfo.getOriginalValue() != null && assetInfo.getResidualRate() != null && assetInfo.getEstimatedLife() != null) {
           Double originalValue =  Double.valueOf(String.valueOf(assetInfo.getOriginalValue()));
           Double rate = Double.valueOf(String.valueOf(assetInfo.getResidualRate()));
           Double estimatedLife = Double.valueOf(assetInfo.getEstimatedLife());
           Double residualValue = originalValue * rate / 100;
           Double annualDepreciation = (originalValue - residualValue) / estimatedLife;
           if (Double.valueOf(String.valueOf(assetInfo.getMonthlyDepreciation())).compareTo(annualDepreciation)!=0) {
                return R.fail(500,ErrorCode.DISCOUNT_AMOUNT_ERROR.getMessage());
           }
       }

        AssetData assetData = new AssetData();
        AssetDepreciation assetDepreciation = new AssetDepreciation();
        if(assetInfo!=null){
            // 更新资产
            if(assetInfo.getId()!=null) {
                assetData.setAssetId(assetInfo.getId());
                assetData.setOriginalValue(assetInfo.getOriginalValue());
                assetData.setTaxAmount(assetInfo.getTaxAmount());
                assetData.setResidualRate(assetInfo.getResidualRate());
                assetData.setEstimatedLife(assetInfo.getEstimatedLife());
                assetData.setMonthlyDepreciation(assetInfo.getMonthlyDepreciation());
                assetData.setAccumDepreciation(assetInfo.getAccumDepreciation());
                assetData.setMonthlyDepreciation(assetInfo.getMonthlyDepreciation());

                // 更新折旧
                assetDepreciation.setAssetId(assetInfo.getId());
                assetDepreciation.setDepreciationMethod(assetInfo.getDepreciationMethod());
                assetDepreciation.setImpairmentAccount(assetInfo.getImpairmentAccount());
                assetDepreciation.setFixedAssetAccount(assetInfo.getFixedAssetAccount());
                assetDepreciation.setAccumDepreciationAccount(assetInfo.getAccumDepreciationAccount());
                assetDepreciation.setDepreciationExpenseAccount(assetInfo.getDepreciationExpenseAccount());
                assetDepreciation.setTaxAccount(assetInfo.getTaxAccount());
                assetDepreciation.setAssetDisposalAccount(assetInfo.getAssetDisposalAccount());
                assetDepreciation.setPurchaseAccount(assetInfo.getPurchaseAccount());
            }else {
                return R.fail(500,ErrorCode.NULL_PARAMETER.getMessage());
            }
        }else {
            return R.fail(500,ErrorCode.NULL_PARAMETER.getMessage());
        }
        assetDataMapper.updateAsset(assetData);
        assetDepreciationMapper.updateAsset(assetDepreciation);
        return R.ok(assetInfoMapper.updateAssetInfo(assetInfo));
    }
}
